1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.io.Serializable;
22  import java.util.Arrays;
23  
24  /**
25   * A class that implements {@code Comparable} without generics, such as those
26   * found in libraries that support Java 1.4 and before. Our library needs to
27   * do the bare minimum to accommodate such types, though their use may still
28   * require an explicit type parameter and/or warning suppression.
29   *
30   * @author Kevin Bourrillion
31   */
32  @GwtCompatible
33  class LegacyComparable implements Comparable, Serializable {
34    static final LegacyComparable X = new LegacyComparable("x");
35    static final LegacyComparable Y = new LegacyComparable("y");
36    static final LegacyComparable Z = new LegacyComparable("z");
37  
38    static final Iterable<LegacyComparable> VALUES_FORWARD
39        = Arrays.asList(X, Y, Z);
40    static final Iterable<LegacyComparable> VALUES_BACKWARD
41        = Arrays.asList(Z, Y, X);
42  
43    private final String value;
44  
45    LegacyComparable(String value) {
46      this.value = value;
47    }
48  
49    @Override
50    public int compareTo(Object object) {
51      // This method is spec'd to throw CCE if object is of the wrong type
52      LegacyComparable that = (LegacyComparable) object;
53      return this.value.compareTo(that.value);
54    }
55  
56    @Override public boolean equals(Object object) {
57      if (object instanceof LegacyComparable) {
58        LegacyComparable that = (LegacyComparable) object;
59        return this.value.equals(that.value);
60      }
61      return false;
62    }
63  
64    @Override public int hashCode() {
65      return value.hashCode();
66    }
67  
68    private static final long serialVersionUID = 0;
69  }